package com.guocheng.modules.login.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.login.model.dto.DeptDTO;
import com.guocheng.modules.login.model.po.Dept;
import com.guocheng.modules.login.model.po.Role;
import com.guocheng.modules.login.service.DeptService;
import com.guocheng.modules.login.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色页面
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "sys/role/list";
    }

    /**
     * 角色页面数据
     * @param role
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult getlist(Role role){
        if(role.getName()!=null&&!role.getName().equals(""))
            role.setName("like_"+role.getName());
        IPage<Role> rolePage = roleService.selectPage(role);
        return AjaxResult.ok().data(rolePage.getRecords()).put("total",rolePage.getTotal());
    }

    /**
     * 用户授权角色列表（不分页,直接获取全部）
     * @return
     */
    @GetMapping("/getlist")
    @ResponseBody
    public AjaxResult getlist(){
        List<Role> roles = roleService.selectList();
        return AjaxResult.ok().data(roles);
    }

    /**
     * 添加角色页面
     * @return
     */
    @GetMapping("/show")
    public String show(@RequestParam(value = "id",required = false)Integer id, Model model){
        if(id!=null){
            Role role = roleService.selectById(id);
            model.addAttribute("role",role);
        }
        return "sys/role/show";
    }

    /**
     * 添加角色提交
     * @param role
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(Role role){
        boolean insert = roleService.insert(role);
        if(!insert){
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.ok("添加成功");
    }

    /**
     * 修改角色提交
     * @param role
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Role role){
        if(role.getId()==null||role.getId().intValue()==0){
            return AjaxResult.error();
        }
        boolean b = roleService.updateById(role);
        return AjaxResult.ok();
    }

    /**
     * 删除角色提交
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("ids")List<Integer> ids){
        int i = roleService.deleteByIds(ids);
        return AjaxResult.ok();
    }

    /**
     * 打开菜单授权页面
     * @return
     */
    @GetMapping("/menu")
    public String menu(){
        return "sys/role/menu";
    }

    /**
     * 获取角色的菜单权限
     * @param id
     * @return
     */
    @GetMapping("/menu/list")
    @ResponseBody
    public AjaxResult quertMenu(@RequestParam("id")Integer id){
        List<Integer> lists = roleService.selectMenuByRoleId(id);
        return AjaxResult.ok().data(lists);
    }

    /**
     * 菜单授权提交
     * @param id    角色id
     * @param ids   菜单ids
     * @return
     */
    @PostMapping("/menu")
    @ResponseBody
    public AjaxResult setMenu(@RequestParam("id")Integer id, @RequestParam(value = "ids",required = false)List<Integer> ids){
        boolean b = roleService.insertRoleMenu(id, ids);
        if (b) {
            return AjaxResult.ok("授权成功");
        }
        return AjaxResult.error();
    }

}
